package com.livestocksolutions.util;

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

/* -- Version History --
 *  1.0 : 10 September 2012 - Initial version.
 */

/**
 * Allows saving a Two dimensional String array into a Parcelable Object.
 * @author Adam Parr
 * @version 1.0
 * 
 * @.modified 10 September 2012
 */
public class StringArrayParcelable implements Parcelable{
	/**
	 * The String Array in which to place into a Parcelable.
	 */
    public String[][] strings;
    
    /**
     * Constructor
     */
    public StringArrayParcelable() {
        strings = new String[1][1];
    }
    
    /**
	 * Get the Array of Strings.
	 * @return The Array of Strings given to this Parcelable.
	 */
    public String[][] getStrings() {
        return strings;
    }
    
    /**
     * Set an Array of Strings to retreive at a later time.
     * @param strings The Array of Strings to give to this Parcelable.
     */
    public StringArrayParcelable setStrings(String[][] strings) {
        this.strings = strings;
        
        return this;
    }

    /**
     * Store the array of Strings in the given Parcel.
     * @param in Parcel to store the Array of Strings.
     */
    public StringArrayParcelable(Parcel in) {
    	
    	ArrayList<StringParcelable> a = new ArrayList<StringParcelable>();
    	in.readTypedList(a, StringParcelable.CREATOR);
    	
    	strings = new String[a.size()][];
    	
    	for(int x=0; x<strings.length; x++){
    		strings[x]=a.get(x).getStrings();
    	}
    }


    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
    	try{
	    	ArrayList<StringParcelable> a = new ArrayList<StringParcelable>();
	    	for(int x=0; x<strings.length; x++){
	    		StringParcelable p = new StringParcelable();
	    		p.setStrings(strings[x]);
	    		a.add(p);
	    	}
	    	dest.writeTypedList(a);
    	}catch(StackOverflowError e){
    		e.printStackTrace();
    	}
    }
    public static final Parcelable.Creator<StringArrayParcelable> CREATOR = new Parcelable.Creator<StringArrayParcelable>() {
        public StringArrayParcelable createFromParcel(Parcel in) {
            return new StringArrayParcelable(in);
        }

        public StringArrayParcelable[] newArray(int size) {
            return new StringArrayParcelable[size];
        }
    };
}

